<?php 

class ContentsController extends AppController {
	var $name = 'Contents';
	var $layout = 'admin';
	var $helpers = array('Javascript', 'Session');
	var $components = array('Auth', 'Session', 'Captcha');
	
	var $lang = '';
	
	function beforeFilter(){
		$this->Auth->allow('underconstruction', 'display', 'home', 'send_requirements', 'captcha');
		$username = $this->Auth->user();
		$this->set('username', $username['User']['username']);
		
	}
	
	public function index(){
		
		$params['order'] = 'id';
		if(isset($this->params['named']['orderBy']) ){
			$params['order'] = $this->params['named']['orderBy'];
			$this->set('orderBy', $this->params['named']['orderBy']);
			if(isset($this->params['named']['DESC'])){
				$params['order'] .= ' DESC';
				$this->set('DESC', $this->params['named']['DESC']);
			}
		}
		$contents = $this->Content->find('all', $params);
		$this->set('contents', $contents);
	}
	
	public function underconstruction(){
		$this->layout = '';
	}
	
	public function display(){
		
		$this->lang = (!empty($this->params['language']) ) ? $this->params['language'] : 'rus';
		$this->set('lang', $this->lang);
		
		$slug = $this->params[ 'slug' ] = (isset($this->params[ 'slug' ])) ? $this->params[ 'slug' ] : 'home' ;
		$this->set('slug', $slug);
		
		$content = $this->Content->find('all', array('conditions'=>array('name'=>$slug)));
		$this->set('content', (isset($content[0]))?$content[0]:null);
		if(isset($content[0])){
			$this->set('title_for_layout', $content[0]['Content']['title_'.$this->lang]);
			$this->set('keywords', $content[0]['Content']['keywords_'.$this->lang]);
			$this->set('description', $content[0]['Content']['description_'.$this->lang]);
			
		}
		
		//set header block content
		$header_block = $this->Content->find('first', array('conditions' => array('name' => 'header_block')));
		$this->set('header_block', ($header_block)?$header_block['Content'][$this->lang]:null);

		$this->Menu =& ClassRegistry::init('Menu');
		$this->set('menus', $this->Menu->find('all', array('order'=>'term')));
		
		$this->Asset =& ClassRegistry::init('Asset');
		$this->set('assets', $this->Asset->find('all', array('conditions' => array('group'=>'slides'))));
		
		
		if( strstr($slug, 'about') ){
			$this->Photo =& ClassRegistry::init('Photo');
			$this->set('photos', $this->Photo->find('all'));
		}
		
		$this->layout = 'public';
		
	}
	
	public function delete($id=NULL){
		if ($id) {
			if($this->Content->delete($id)){
				$this->Session->setFlash('Content deleted successfuly!');
				$this->redirect($this->referer());
			}
			else{
				$this->Session->setFlash('Failed! Please try again or contact to administrator.');
				$this->redirect($this->referer());
			}
			
		}
		else{
			$this->Session->setFlash('Failed! No such content.');
			$this->redirect($this->referer());
			
		}
	}
	
	public function switchis_block($id=NULL){
		if($id){
			if($this->Content->findById($id)){
				$this->Content->id = $id;
				$new = ($this->Content->field('is_block') + 1) % 2;
				if($this->Content->saveField('is_block', $new)){
					$this->Session->setFlash('Change affected successfuly!');
					$this->redirect(array('action' => 'index'));
				}
			}
			else{
				$this->Session->setFlash('Failed (no such id)! Please try again or contact to administrator.');
				$this->redirect(array('action' => 'index'));
			}
		}
		else{
			$this->Session->setFlash('Failed! No such content.');
			$this->redirect(array('controller' => 'contents', 'action' => 'index'));
			
		}
	}
	
	public function add(){
		if (!empty($this->data)) {
			$this->Content->create();
			if($this->Content->save($this->data)){
				if (isset($this->params['form']['edit'])) {
					$this->Session->setFlash('Content added successfuly!');
					$this->redirect(array('action'=>'edit', $this->Content->id ));
				}else{
					$this->Session->setFlash('Content added successfuly!');
					$this->redirect(array('action'=>'index' ));
				}
			}else{
				$this->Session->setFlash('Failed to add content!');
				$this->redirect(array('action'=>'index' ));
			}
		}
	}
	
	public function edit($id){
		if (!empty($this->data)) {
			$this->Content->id = $id;
			
			$new_blocks = array();
			$new_str = "";
			$content_names = $this->Content->find('list', array('fields'=>'Content.name', 'conditions'=>'is_block = 1'));
			foreach ($content_names as $name){
				if (isset($this->params['form'][$name])) {
					$new_blocks[$this->params['form'][$name]] = $name;
				}
			}
			ksort($new_blocks);
			while (list($key, $val) = each($new_blocks)) {
				if ($new_str != "") $new_str .= ";";
				$new_str .= $val; 
			}
			$this->data['Content']['rightblocks'] = $new_str;
			
			if($this->Content->save($this->data)){
				$this->Session->setFlash('Content edited successfuly!');
				$this->redirect(array('action'=>'index' ));
			}else{
				$this->Session->setFlash('Failed to edit content!');
				$this->redirect(array('action'=>'index' ));
			}
		}else{
			$this->data = $this->Content->findById($id);
		}
	}
	
	public function changeBlocksTerm($block, $cont_selected){
		
		$content = $this->Content->findByName($cont_selected);
		//var_dump($content);
		$str = $content['Content']['rightblocks'];
		$id = $content['Content']['id'];
		  
		$rightblocks = array();
		$rightblocks = explode(";", $str);
	
		$new_blocks = array();
		$new_str = "";
	
		for($i=0; $i<count($rightblocks); $i++){
			if($rightblocks[$i] != $block){ $new_blocks[$i] = $rightblocks[$i]; }
			else {$new_blocks[$i] = $rightblocks[$i+1];$new_blocks[$i+1] = $rightblocks[$i];$i++;}
		}
			
		while (list($key, $val) = each($new_blocks)) {
			if ($new_str != "") $new_str .= ";";
			$new_str .= $val; }
		
		$this->Content->updateAll(
			array('rightblocks' => "'$new_str'"),
			array('name' => $cont_selected)
		);
			
		
		
		$this->Session->setFlash('New term was set!');
		$this->redirect(array('action'=>'edit',  $id));
	}

	/*------start tiny mce functions------------------------*/
	public function getTinyImageList($cont){
		$this->Asset =& ClassRegistry::init('Asset');
		$images = $this->Asset->find('all', array(
			
				'or' => array (
					'category' => 'general',
					'category' => $cont
				    )
				)
			
		);
		
		$this->set('images', $images);
		$this->layout = '';
		//$this->render('/elements/empty');
	}
	
	public function getTinySwfList(){
		$this->Asset =& ClassRegistry::init('Asset');
		$images = $this->Asset->find('all');
		
		$this->set('images', $images);
		$this->layout = '';
		//$this->render('/elements/empty');
	}
	
	public function getTinyLinkList(){
		$links = $this->Content->find('all', array('conditions'=>'is_block = 0'));
		echo 'var tinyMCELinkList = new Array(';
		
		$i=0;
		foreach ($links as $link){
			echo '	["'.$link['Content']['name'].'", "'. APP_ROOT .'/pages/' . $link['Content']['name'].'"]'; 
			if($i != (count($links)-1)) echo ","; 
			$i++;
		}

		echo ');';
		
		
		$this->layout = '';
		$this->render('/elements/empty');
	}
	
	public function getTinyTemplateList(){
		
		echo '
			var tinyMCETemplateList = [
				// Name, URL, Description
				["Block", "'. APP_ROOT .'/templates/block.htm", "Block."],
				["Hot Offers", "'. APP_ROOT .'/templates/block2.htm", "Hot Offers"]
			];
		';
		
		
		$this->layout = '';
		$this->render('/elements/empty');
	}
	/*------end tiny mce functions------------------------*/

	public function send_requirements($param){
		
		if(empty($this->params['form']['tel']) && empty($this->params['form']['email'])){
			$this->Session->setFlash('E-mail or tel. must be filled!');
		}else{
			$msg = 
			' type: ' . (!empty($this->params['form']['type']) ? $this->params['form']['type'] : 'N/A')
			.' address: ' . (!empty($this->params['form']['address']) ? $this->params['form']['address'] : 'N/A')
			.' rooms: ' . (!empty($this->params['form']['rooms']) ? $this->params['form']['rooms'] : 'N/A')
			.' floar: ' . (!empty($this->params['form']['floar']) ? $this->params['form']['floar'] : 'N/A')
			.' sqm: ' . (!empty($this->params['form']['sqm']) ? $this->params['form']['sqm'] : 'N/A')
			.' price: ' . (!empty($this->params['form']['price']) ? $this->params['form']['price'] : 'N/A')
			.' tel: ' . (!empty($this->params['form']['tel']) ? $this->params['form']['tel'] : 'N/A')
			.' email: ' . (!empty($this->params['form']['email']) ? $this->params['form']['email'] : 'N/A')
			.' additional: ' . (!empty($this->params['form']['additional']) ? $this->params['form']['additional'] : 'N/A')
			;
			
			@mail('info@irentals.am', 'Send your requirements: ' . $param, $msg,
			 "From: info@irentals.am \r\n" .
			 "MIME-Version: 1.0\r\n" .
		 	 "Content-type: text/html; charset=utf-8\r\n");
			$this->Session->setFlash('mail sent successfully');
		}
		$this->redirect($this->referer());
		
		$this->layout = '';
		$this->render('/elements/empty');
	}
	
	
	function captcha() 
    { 
        $this->Captcha->render(); 
    } 
}

?>